package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.SysGeographical;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.mapper.SysGeographicalMapper;
import com.ruoyi.system.service.ISysGeographicalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 地理位置信息Service业务层处理
 * 
 * @author yfkj
 * @date 2021-10-12
 */
@Service
public class SysGeographicalServiceImpl extends ServiceImpl<SysGeographicalMapper, SysGeographical> implements ISysGeographicalService
{
    @Autowired
    private SysGeographicalMapper sysGeographicalMapper;

    /**
     * 查询地理位置信息
     * 
     * @param geographicalId 地理位置信息主键
     * @return 地理位置信息
     */
    @Override
    public SysGeographical selectSysGeographicalByGeographicalId(Long geographicalId)
    {
        return sysGeographicalMapper.selectSysGeographicalByGeographicalId(geographicalId);
    }

    /**
     * 查询地理位置信息列表
     * 
     * @param sysGeographical 地理位置信息
     * @return 地理位置信息
     */
    @Override
    public List<SysGeographical> selectSysGeographicalList(SysGeographical sysGeographical)
    {
        return sysGeographicalMapper.selectSysGeographicalList(sysGeographical);
    }

    /**
     * 新增地理位置信息
     * 
     * @param sysGeographical 地理位置信息
     * @return 结果
     */
    @Override
    public int insertSysGeographical(SysGeographical sysGeographical)
    {
        return sysGeographicalMapper.insertSysGeographical(sysGeographical);
    }

    /**
     * 修改地理位置信息
     * 
     * @param sysGeographical 地理位置信息
     * @return 结果
     */
    @Override
    public int updateSysGeographical(SysGeographical sysGeographical)
    {
        return sysGeographicalMapper.updateSysGeographical(sysGeographical);
    }

    /**
     * 批量删除地理位置信息
     * 
     * @param geographicalIds 需要删除的地理位置信息主键
     * @return 结果
     */
    @Override
    public int deleteSysGeographicalByGeographicalIds(Long[] geographicalIds)
    {
        return sysGeographicalMapper.deleteSysGeographicalByGeographicalIds(geographicalIds);
    }

    /**
     * 删除地理位置信息信息
     * 
     * @param geographicalId 地理位置信息主键
     * @return 结果
     */
    @Override
    public int deleteSysGeographicalByGeographicalId(Long geographicalId)
    {
        return sysGeographicalMapper.deleteSysGeographicalByGeographicalId(geographicalId);
    }

    @Override
    public List<SysGeographical> buildGeographicalTree(List<SysGeographical> geographicalList) {
        List<SysGeographical> returnList = new ArrayList<SysGeographical>();
        List<Long> tempList = new ArrayList<Long>();
        for (SysGeographical geographical : geographicalList)
        {
            tempList.add(geographical.getGeographicalId());
        }
        for (Iterator<SysGeographical> iterator = geographicalList.iterator(); iterator.hasNext();)
        {
            SysGeographical sysGeographical = (SysGeographical) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(sysGeographical.getParentId()))
            {
                recursionFn(geographicalList, sysGeographical);
                returnList.add(sysGeographical);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = geographicalList;
        }
        return returnList;
    }

    @Override
    public List<TreeSelect> buildGeographicalTreeSelect(List<SysGeographical> geographicalList) {
        List<SysGeographical> geographicalTrees = buildGeographicalTree(geographicalList);
        return geographicalTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public SysGeographical getGeographicalByName(String name) {
        return sysGeographicalMapper.getGeographicalByName(name);
    }

    @Override
    public SysGeographical getGeographicalByCode(String code) {
        return sysGeographicalMapper.getGeographicalByCode(code);
    }

    @Override
    public SysGeographical getGeographicalById(Long id) {
        return sysGeographicalMapper.getGeographicalById(id);
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<SysGeographical> list, SysGeographical t)
    {
        // 得到子节点列表
        List<SysGeographical> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysGeographical tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<SysGeographical> getChildList(List<SysGeographical> list, SysGeographical t)
    {
        List<SysGeographical> tlist = new ArrayList<>();
        Iterator<SysGeographical> it = list.iterator();
        while (it.hasNext())
        {
            SysGeographical n = (SysGeographical) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getGeographicalId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysGeographical> list, SysGeographical t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }
}
